package com.lyf.core.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.viewbinding.ViewBinding;

import com.lyf.core.presenter.BasePresenter;
import com.trello.rxlifecycle4.android.FragmentEvent;

public abstract class BaseMvpFragment<VB extends ViewBinding, T extends BasePresenter> extends BaseFragment<VB> {

    protected T mPresenter;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mPresenter = getPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
            mPresenter.addObserver(bindUntilEvent(FragmentEvent.DESTROY_VIEW));
        }

        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null) {
            refreshData();
        }
    }

    public void refreshData() {

    }

    public abstract T getPresenter();

    public void moRefreshData() {
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}
